<?php

class admin_app_app extends admin_app
{

	public function __construct()
	{    
		$my = bone::get_admin();
		if($my->guest)$this->redirect('./?app=user&task=login', '登陆超时', 'error');
		
		parent::__contruct( 'app', '添加/删除程序' );
	}
	

	public function listing()
	{
		$model = $this->get_model('app');
		$app = $model->get_list();

		$template = $this->get_template('listing');
		$template->set_title('已安装的程序');
		$template->set('app', $app);
		$template->set_data($this->get_data());
		$template->display();
	}

	
	public function online_listing()
	{
		$key = get('key');
		$template = $this->get_template('online_listing');

		$config = $this->get_config();
		$soap = new SoapClient(null, array('location'=>$config->uri_app, 'uri'=>$config->uri));

		$limit = 8;
		
		$pagination = $template->get_pagination();
		$pagination->set_limit( $limit );
		$pagination->set_page( get('page', 1, 'int') );
		if($key)
		{
			$pagination->set_total( $soap->get_search_total($key) );
			$pagination->set_url('./?app=app&task=online_listing&key='.$key);
			$app_list = $soap->get_search_list($key, $pagination->get_offset(), $limit);
		}
		else
		{
			$pagination->set_total( $soap->get_total() );
			$pagination->set_url('./?app=app&task=online_listing');
			$app_list = $soap->get_list($pagination->get_offset(), $limit);
		}

		$model = $this->get_model('app');
		$installed_app_list = $model->get_list();
		$installed_app = array();
		foreach($installed_app_list as $app)
		{
			$installed_app[] = $app->app_id;
		}

		$template->set_title('安装新应用');
		$template->set('app_list', $app_list);
		$template->set('installed_app', $installed_app);
		$template->set_data($this->get_data());
		$template->display();
	}


	public function ajax_install()
	{
		$app_id = get('app_id', 0, 'int');
		if($app_id==0)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{app_id}缺失');
			$this->ajax();
		}

		$model_app = $this->get_model('app');
		if(!$model_app->install($app_id))
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', $model_app->get_error() );
			$this->ajax();
		}

		// 安装成功
		$this->set_ajax('status', 0);
		$this->set_ajax('description', '安装成功');
		$this->ajax();
	}


	public function ajax_delete()
	{
		$app = get('app', '');
		if($id=='')
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{app}缺失');
			$this->ajax();
		}

		if($id<=1024)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '系统自带的程序不能删除');
			$this->ajax();
		}

		$model = $this->get_model('app');
		if( $model->uninstall($app) )
		{
			$this->set_ajax('status', 0);
			$this->set_ajax('description', '删除成功');
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}
	}


	
}
?>